﻿using System;
using CardLogistics.Model.Sys;
using CardLogistics.Web.Components;
using System.Data;

namespace CardLogistics.Web.MainPage.SystemMgr.SysRole
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.Sys.Sys_SysRolesData DAL = new DAL.Sys.Sys_SysRolesData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void InitControl()
        {
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                    Sys_SysRoles rData = DAL.GetModel(_key);
                    SetWebControls(this.Page, rData);
                    hdfSortCode.Value = rData.SORTCODE.ToString();//排序码
                    if (rData.FULLNAME.Equals("系统管理员"))
                    {
                        FULLNAME.Attributes.Add("readonly", "readonly");
                    }
                }
            }
            else
                CODE.Value = GetMAXNO("SYS_SYSROLES", "CODE", " and DELETEMARK=1 ");
        }
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = "";
                string logContent = "";
                if (IsEdit)
                {
                    result = DAL.Update(EncapsulationModel());
                    logContent = "修改角色信息,角色名称:" + EncapsulationModel().FULLNAME;
                }
                else
                {
                    result = DAL.Add(EncapsulationModel());
                    logContent = "添加角色信息,角色名称:" + EncapsulationModel().FULLNAME;
                }
                if (result.Split(',')[0] == "ok")
                {
                    InsertLog(logContent);
                    ReturnListInfo();
                }
                else
                {
                    ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Sys_SysRoles EncapsulationModel()
        {
            Sys_SysRoles model = new Sys_SysRoles();
            model = GetWebControls<Sys_SysRoles>(this.Page);
            model.CREATEUSERID = LoggedUser.ID;
            //是否启用 0 是 1 否 
            if (Enabled.Checked)
                model.ENABLED = "1";//否
            else
                model.ENABLED = "0";//是
            model.DELETEMARK = "1";
            if (IsEdit)
            {
                model.ROLEID = Request.QueryString["id"];
                //获取排序码
                model.SORTCODE = Convert.ToInt32(hdfSortCode.Value);
            }
            else
            {
                //获取最大的排序码
                DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult("select max(SortCode)as MaxSortCode from Sys_SysRoles");
                if (dt.Rows.Count > 0)
                {
                    int maxSortCode = Convert.ToInt32(dt.Rows[0]["MaxSortCode"].ToString());
                    //获取新的排序码
                    model.SORTCODE = maxSortCode + 1;
                }
            }
   
            return model;
        }
    }
}